package edu.arizona.eller.mis.liferoulette;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class result_maps extends MapActivity{
	LocationManager locationManager;
	Location location;
	MapView mapView;
	MapController mapController;

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result_maps);
        mapView = (MapView) findViewById(R.id.map1);
        mapView.setBuiltInZoomControls(true);
        
		mapController = mapView.getController();
		mapController.setZoom(14); 
		
		Drawable marker = getResources().getDrawable(R.drawable.pinpoint);
		marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
		GeoMarker geoMarker = new GeoMarker(marker,this);
		

        Intent i = getIntent();
        double lat = i.getDoubleExtra("lat",0); 
        double lon = i.getDoubleExtra("lon",0); 
        String name = i.getStringExtra("name");
        String vicinity = i.getStringExtra("vicinity");


			if(lat != 0 && lon != 0){
	        	
	        	GeoPoint point = new GeoPoint((int)(lat*1E6), (int)(lon*1E6));
	        	
	        	geoMarker.locations.add(new OverlayItem(point, name, vicinity));
	        	geoMarker.Populate();
	        	mapView.getOverlays().add(geoMarker);
	        	
	        	mapController.setCenter(point);
	        	mapController.animateTo(point);
	        }

        
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return true;
	}
	

	class GeoMarker extends ItemizedOverlay<OverlayItem>{

		@Override
		protected boolean onTap(int index) {
			// TODO Auto-generated method stub
			OverlayItem item = locations.get(index);
			
			Builder b = new AlertDialog.Builder(context).setMessage(item.getSnippet()).setTitle(item.getTitle()).setIcon(R.drawable.map);
			b.setNegativeButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					dialog.cancel();
				}
			}).create();
			b.show();
			/*AlertDialog.Builder dialog = new AlertDialog.Builder(context);
			dialog.setTitle(item.getTitle())
			dialog.setMessage(item.getSnippet());
			dialog.show();*/
			return true;
		}

		private ArrayList<OverlayItem> locations = new ArrayList<OverlayItem>();
		private Drawable marker;
		private Context context;
		
		public GeoMarker(Drawable marker, Context context) {
			super(marker);
			// TODO Auto-generated constructor stub
			this.marker = marker;
			this.context = context;
			populate();
		}
		
		public void Populate(){
			populate();
		}
		
		@Override
		public void draw(Canvas canvas,MapView mapView,boolean shadow){
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
		}

		@Override
		protected OverlayItem createItem(int i) {
			// TODO Auto-generated method stub
			return locations.get(0);
		}

		@Override
		public int size() {
			// TODO Auto-generated method stub
			return locations.size();
		}
		
		
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.activity_menu, menu);
	    return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.settingsItem:     
	        	startActivity(new Intent(this, UserPreferenceAcvitity.class));
	            break;
	        case R.id.historyItem:     
	        	startActivity(new Intent(this, HistoryActivity.class));
	            break;
	        case R.id.homeItem:     
	        	startActivity(new Intent(this, main.class));
	            break;
	    }
	    return true;
	}
}
